package com.mimaxueyuan.disruptor.demo07;

import com.lmax.disruptor.EventHandler;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Once we have the event defined we need to create a consumer that will handle these events.
 * In our case all we want to do is print the value out the the console.
 *
 * @author: Kevin
 * @官网: 	www.mimaxueyuan.com
 * @Q Q群:	660567408
 * @Email:	mimaxueyuan@163.com
 * [每天进步一点点、人生带来大改变...]
 * [本代码对应视频地址:http://study.163.com/course/introduction/1004573050.htm]
 */
public class LongEventHandler1 implements EventHandler<LongEvent> {

	/**
	 * event:发布到RingBuffer中的事件
	 * sequence:当前正在处理的事件序列号
	 * endOfBatch:是否为Ringbuffer的最后一个
	 */
	@Override
	public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
		ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(10);
		AtomicInteger count = new AtomicInteger();
		String scheduleName = "first";
		ScheduledFuture schedule= scheduledExecutorService.scheduleAtFixedRate(()->{
			count.getAndIncrement();
			System.out.println("------------定时任务------");
			if(count.get() == 5 ){//检测到合格的值
				System.out.println("------------符合条件------");
				event.setPrevState(0);
				event.setCurrentState(100);
				event.setValue(100);
				event.getThreadMap().get(scheduleName).cancel(false);
			}
		},1,2, TimeUnit.SECONDS);
		event.getThreadMap().put(scheduleName,schedule);
		while(event.getCurrentState()!=100){
			System.out.println("-----判断结果-----"+ event.getCurrentState());
		}
	}

}
